import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:数组
 * User: LianBao
 * Date: 2024-02-04
 * Time: 19:48
 */
public class Test {

    public static int[] func3() {
        //Java中可以返回数组
        int[] tmp = {1, 2, 3, 4, 5, 6, 7};
        return tmp;
    }

    public static void main(String[] args) {
        // 方法的返回值可以是数组
        int[] arr = {1, 2, 3, 4};
        System.out.println(Arrays.toString(arr));
        arr = func3();
        System.out.println(Arrays.toString(arr));
    }


    public static void func1(int[] arr) {
        arr = new int[10];
    }

    public static void func2(int[] arr) {
        arr[0] = 99;
    }

    public static void main9(String[] args) {
        int[] arr = {1, 2, 3, 4};
//        func1(arr);
//        System.out.println(Arrays.toString(arr)); //1,2,3,4
        func2(arr);
        System.out.println(Arrays.toString(arr)); //99,2,3,4
    }

    public static void main8(String[] args) {
        //
        int[] arr = null;
        //null代表引用类型的0,代表这个引用不指向任何对象
        System.out.println(arr);
    }

    public static void main7(String[] args) {
        int[] arr1 = {1, 2, 3, 4};
        int[] arr2 = {5, 6, 7, 8, 9};
        arr2 = arr1;
        //arr1的值(地址)赋给arr2,arr2原来的内存没人引用了,自动被jvm回收了
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
    }

    public static void main6(String[] args) {
        int[] arr1 = {1, 2, 3, 4};
        System.out.println(Arrays.toString(arr1));
        int[] arr2 = arr1;
        arr2[1] = 99;
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));
        //arr2这个引用指向了arr1这个引用 所引用的对象

    }

    public static void main5(String[] args) {
        int[] arr = {1, 2, 3, 4};
        //arr这个引用指向一个对象(地址)
        System.out.println(arr);//地址[I@4eec7777
        //局部变量在栈上,数组在堆上

    }

    public static void main4(String[] args) {
        //三种定义数组的方式:
        //1.
        int[] arr1 = {1, 2, 3, 4};
        int[] arr2 = new int[]{1, 2, 3, 4};
        int[] arr3 = new int[10];//分配大小,里面默认值是0

    }

    public static void main3(String[] args) {
        int[] array = new int[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
        String ret = Arrays.toString(array);
        System.out.println(ret);

    }

    public static void main2(String[] args) {
        int[] arr = {1, 2, 3, 4};
        //遍历数组:
        //1.
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();

        //2.拿不到下标
        for (int x : arr) {
            System.out.print(x + " ");
        }
        //3.使用工具Arrays
        String ret = Arrays.toString(arr);//把数组转变为字符串返回
        System.out.println(ret);

    }

    public static void main1(String[] args) {
        int[] array1 = {1, 2, 3};//初始化(静态)
        float[] array2 = {1.0f, 2.5f};
        System.out.println(array2[1]);//下标访问数组元素,下标从0开始
        System.out.println(array1[2]);
        int len = array1.length; //输出数组长度:数组名.length
        System.out.println(len);
        int[] array3 = new int[]{1, 2, 3, 4};//另一种初始化方式(动态)
        /*
        两种写法没区别,
         */

        int[] array4 = new int[10]; //只分配了内存,没有初始化,里面的值默认是0,boolean类型默认是false,如果是引用类型,默认是null


        int[] array5;
        array5 = new int[]{1, 2, 3, 4, 5};
    }

}
